import service from './server'
import { CONTENT_TYPE } from '@/constants'
import { useUserStoreWithOut } from '@/store/modules/user'

const request = (option) => {
  const { url, method, params, data, headers = {}, responseType } = option
  const userStore = useUserStoreWithOut()

  return service.request({
    url,
    method,
    params,
    data,
    responseType,
    headers: {
      'Content-Type': CONTENT_TYPE,
      [userStore.getTokenKey || 'Authorization']: userStore.getToken || '',
      ...headers
    }
  })
}
export const rawRequest = request 

export default {
  get(option) {
    return request({ method: 'get', ...option })
  },
  post(option) {
    return request({ method: 'post', ...option })
  },
  delete(option) {
    return request({ method: 'delete', ...option })
  },
  put(option) {
    return request({ method: 'put', ...option })
  },
  cancelRequest(url) {
    return service.cancelRequest(url)
  },
  cancelAllRequest() {
    return service.cancelAllRequest()
  },
  request,
}
